package com.ruoyi.web.pojo.request.devicedata;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.web.domain.warning.FactorInfo;
import com.ruoyi.web.enums.AggreEnum;
import com.ruoyi.web.enums.DeviceDataCountEnum;
import com.ruoyi.web.pojo.request.insect.InsectListRequest;
import com.ruoyi.web.utils.influxdb.DeviceAggreDataParam;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Data
public class DeviceFactorDataRequest {

    @ApiModelProperty(value = "气象墒情-设备ID")
    @NotEmpty(message = "设备id不能为空")
    private List<String> deviceIds;

    @ApiModelProperty(value = "杀虫或虫情 - 设备ID")
    // @NotEmpty(message = "设备id不能为空")
    private List<String> insectDeviceIds;

    @ApiModelProperty(value = "数据开始时间,格式 yyyy-MM-dd")
    private String startTime;

    @ApiModelProperty(value = "数据结束时间")
    private String endTime;

    @ApiModelProperty(value = "因子 杀虫分析 传 dct")
    private List<String> factors;

    @ApiModelProperty(value = "1 杀虫 2 识虫 - 综合分析使用")
    private Integer contrastType;

    @ApiModelProperty(value = "昆虫名字 - 综合分析使用")
    private String contrastInsect;

    @ApiModelProperty(value = "要查询的设备 + 传感器因子")
    private List<FactorDataFilter> filterList;
    /**
     * @desc  得到因子开窗数据查询对象
     * @author fwx 2024/3/8 15:58
     **/
    public DeviceAggreDataParam getDeviceAggreDataParam(String deviceId, Long productId, FactorInfo factorInfo , String factor){
        DeviceAggreDataParam param = new DeviceAggreDataParam();
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        param.setFactor(factor);
        param.setDeviceId(deviceId);
        param.setProductId(productId);

        Set<String> countItem = factorInfo.buildCountItem();

        if(countItem.contains(DeviceDataCountEnum.SUM.name())){
            param.setType(AggreEnum.SUM);
        }else {
            param.setType(AggreEnum.AVG);
        }

        return param;
    }
    
    /**
     * @desc  得到昆虫数据查询对象
     * @author fwx 2024/3/11 15:09
     **/
    public InsectListRequest getInsectListRequest(){
        InsectListRequest request = new InsectListRequest();

        request.setInsectName(contrastInsect);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setDeviceIds(deviceIds);

        return request;
    }

    /**
     * @desc  判断是否查询该设备的因子数据
     * @author fwx 2024/5/21 16:17
     **/
    public boolean compareFactor(String deviceId, String factorName){

        if(CollUtil.isEmpty(filterList)){
            return true;
        }

        for (FactorDataFilter filter : filterList) {
            if(filter.check(deviceId,factorName)){
                return true;
            }
        }

        return false;
    }
}


@Data
class FactorDataFilter{
    String deviceId;
    String factorKey;

    public boolean check(String deviceId, String factorKey){
        return Objects.equals(this.deviceId,deviceId) && Objects.equals(this.factorKey,factorKey);
    }
}
